package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/16.<br><br>
 *
 * 字符串中的单词数
 */
public class Topic434 {

    public static void main(String[] args) {
        Topic434 instance = new Topic434();

        System.out.println(instance.countSegments("Hello, my name is John"));
        System.out.println(instance.countSegments("Hello"));
        System.out.println(instance.countSegments("love live! mu'sic forever"));
        System.out.println(instance.countSegments(""));
    }

    public int countSegments(String s) {
        int result = 0;
        for(int i = 0; i < s.length(); i++) {
            if((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') {
                result++;
            }
        }

        return result;
    }
}
